//
//  FDtotalView.m
//  FamousDoctorOnline
//
//  Created by 张睿 on 16/7/8.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDtotalView.h"
#import <Masonry.h>
#import "FDDocterInfoController.h"
@interface FDtotalView ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>


@property (nonatomic, assign)int index;

@property (nonatomic, strong)UITableView * leftListTable;
@property (nonatomic, strong)UITableView * rightListTable;

@end

@implementation FDtotalView


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {

        [self setupMiddleView];
        [self setupLayout];
        
    }
    
    return self;
}


- (void)setupLayout{
    [self.bigView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.leftListTable mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(self.rightListTable.mas_leading).mas_offset(0);
        
        make.height.width.mas_equalTo(self.bigView);
    }];
    [self.rightListTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);

        make.trailing.mas_equalTo(self.bigView.mas_trailing).mas_offset(0);
        
        make.height.width.mas_equalTo(self.bigView);

    }];
}


- (void)setupMiddleView{
    
    UIScrollView * bigView = [[UIScrollView alloc]init];
        bigView.backgroundColor = kThemeColor;
    
    
    
    bigView.delegate = self;
    [self addSubview:bigView];
    bigView.pagingEnabled = YES;
    
//    bigView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    
    UITableView * listTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.leftListTable = listTable;
    listTable.delegate = self;
    listTable.dataSource = self;
    [bigView addSubview:listTable];
    self.bigView = bigView;
    
    UITableView * rightTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.rightListTable = rightTable;
    rightTable.delegate = self;
    rightTable.dataSource = self;
    rightTable.sectionFooterHeight = 15;
    rightTable.sectionHeaderHeight = 0;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    rightTable.tableHeaderView = view;
    [bigView addSubview:rightTable];
   
//    self.index = self.bigView.contentOffset.x / SCREEN_WIDTH;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.rightListTable == tableView) {
        return 5;
    }else{
        return 1;
    }
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.rightListTable == tableView) {

        return 1;
    }else{
        return 10;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell;
    
    if (tableView == self.leftListTable) {
    
        cell = [tableView dequeueReusableCellWithIdentifier:@"left"];
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"left"];
        }
        
        cell.textLabel.text = [NSString stringWithFormat:@"就诊条件:%ld行",(long)indexPath.row];;

    }else{
        
        cell  = [tableView dequeueReusableCellWithIdentifier:@"right"];
       
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"right"];
        }
        
         cell.textLabel.text = [NSString stringWithFormat:@"医生简介:%ld行",(long)indexPath.section];
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.rightListTable) {
        return 60;
    }
    
    return 50;
}


- (void)sentText:(offsetBlock)block{
    
    self.sentBlock = block;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    

    if([scrollView isMemberOfClass:[UITableView class]]){
        return;
    }
    self.index = scrollView.contentOffset.x / SCREEN_WIDTH;
    if (self.index == 1 || self.index == 0) {
        if (self.sentBlock) {
            self.sentBlock(self.index);
        }
    }

    

}

@end
